home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13726 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  46 lines

  1. Newsgroups: comp.lang.c
  2. Path: netcom.com!smryan
  3. From: smryan@netcom.com (@#$%!?!)
  4. Subject: Re: How to use assert( )
  5. Message-ID: <smryanDpKxLz.39v@netcom.com>
  6. Organization: The Programmer formerly known as S M Ryan
  7. X-Newsreader: TIN [version 1.2 PL1]
  8. References: <4kc3k7$dur@orion.cybercom.net>
  9. Date: Tue, 9 Apr 1996 05:22:47 GMT
  10. Sender: smryan@netcom18.netcom.com
  11.  
  12.     #include <assert.h>
  13.  
  14.     assert(some_expression_which_must_be_true);
  15.  
  16. as in 
  17.  
  18.     assert(f=fopen("name","r"));
  19.  
  20. If the expression is false, the program is aborted. On a Unix system, it
  21. will print the file name, line number, and the failed expression to stderr.
  22. In the example, if fopen returns a non-null result, the expression will
  23. be true. If the file cannot be openned, the assertion fails.
  24.  
  25. If you look at assert.h on your system, you'll see it's not too complicated.
  26. And you can easily write your own to do a long jump, or pop up a dialog, or
  27. whatever else pleases you.
  28.  
  29.  
  30. And if you are confident or macho enough that you don't want to use run time
  31. to check assertions, you can do something like
  32.  
  33.     #define NDEBUG
  34.     #include <assert.h>
  35.  
  36. The assert(predicate) is elided.
  37.  
  38.  
  39. Unlike some languages, C assertions have no special syntax or semantics.
  40.  
  41. -- 
  42. The Queen, amused, in quiet power,         | smryan@netcom.com  PO Box 1563
  43. will draw the son to darkenned bower.      |          Cupertino, California
  44. Her face is fair, her fragrance rare,      | (xxx)xxx-xxxx            95015
  45. with woven webs for wayward flower.        |         I don't use no smileys
  46.